{include file="public/_header" /}
<style>
    #mainForm {
        max-width: 1000px;
        margin: 30px auto;
    }

    #mainForm .layui-form-item {
        margin-bottom: 25px;
    }
</style>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">添加</div>
        <div class="layui-card-body">
            <form class="layui-form" id="mainForm">
                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">标题(中文):</label>
                    <div class="layui-input-block">
                        <input name="title_zh" id="titleZh" required placeholder="请输入" class="layui-input"
                               lay-verType="tips"/>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">标题(英语):</label>
                    <div class="layui-input-block">
                        <input name="title_en" id="titleEn" required placeholder="请输入" class="layui-input"
                               lay-verType="tips"/>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">标题(日语):</label>
                    <div class="layui-input-block">
                        <input name="title_jp" id="titleJp" required placeholder="请输入" class="layui-input"
                               lay-verType="tips"/>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">标题(韩语):</label>
                    <div class="layui-input-block">
                        <input name="title_kr" id="title_kr" required placeholder="请输入" class="layui-input"
                               lay-verType="tips"/>
                    </div>
                </div>

                <div class="layui-form-item" id="uploadImage">
                    <label class="layui-form-label layui-form-required">上传封面</label>
                    <div class="layui-input-block">
                        <button type="button" class="layui-btn" id="uploadCoverImg">上传图片</button>
                        <input type="hidden" name="img_src" id="img_src">
                    </div>
                    <div class="layui-input-block">
                        <div class="layui-upload-list" id="coverImg"></div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">排序:</label>
                    <div class="layui-input-block">
                        <input name="sort" id="sort" type="number" placeholder="请输入" class="layui-input"
                               lay-verType="tips"/>
                    </div>
                </div>

                <div class="form-group-bottom text-center">
                    <button type="reset" class="layui-btn layui-btn-primary">&emsp;重置&emsp;</button>
                    <button class="layui-btn" lay-filter="add" lay-submit>&emsp;提交&emsp;</button>
                </div>
                <input type="hidden" name="pid" id="pid" value="{$pid}">
            </form>
        </div>
    </div>
</div>
{include file="public/_footer"}
<script>
    var $,admin;
    var index= 0;

    layui.use(['form', 'element','admin','upload'], function () {
        var form = layui.form;
        $ = layui.jquery;
        admin = layui.admin;
        var upload = layui.upload;

        var pid = $('#pid').val();

        upload.render({
            elem: '#uploadCoverImg'
            , url: '/admin/upload/upload'
            , multiple: false
            ,accept : 'images'
            , before: function (obj) {
            }
            , done: function (res) {
                //上传完毕
                if (res.code === 0) {
                    var html = '<img src="' + res.src + '" width="100px" height="100px" class="layui-upload-img">';
                    $('#coverImg').html(html);
                    $('#img_src').val(res.src);
                }
            }
        });


        // 提交
        form.on('submit(add)', function (data) {
            var arr = data.field;
            // 提交数据
            $.post('{:url("add")}', arr, function (res) {
                if (res.code === 0) {
                    layer.msg(res.msg);
                    setTimeout(function () {
                        window.location.href = "{:url('index')}?pid=" + pid;//'index.html';
                    }, 500);
                } else {
                    layer.msg(res.msg);
                }
            }, 'JSON');
            return false;
        });
    });
</script>